home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Multimedia & Desktop / sk8 / SK8InJava / SK8Script to Java Notes < prev   
Encoding:
Text File  |  1997-02-27  |  8.8 KB  |  98 lines  |  [TEXT/ttxt]

  1. ==========================================
  2. SK8 1.1 SK8Script To Java Notes (v. 1.0)
  3. Brian Roddy
  4. © 1997 Apple Computer, Inc.
  5. ==========================================
  6.  
  7.  
  8. Introduction and Caveats
  9. ------------------------
  10. In order to facilitate those users that wish to move to Java we have created a set of conversion utilities and a set of SK8-like classes in Java.  Java is a very, very different environment then SK8 for a number of reasons.  This precludes a completely automated process.  Our hope is simply to do a 50 to 70% translation of your code to make your life easier.  To put all this another way, don't get your hopes up that you can push a button and have Java.  You'll have to get fairly fluent in Java to properly use this.  But in the meanwhile, bask in the fact we help alleviate the tedium of porting.
  11.  
  12. The Java conversion will take a set of SK8Script text files and convert them to a set of Java class files.  It will create a class for every object that has handlers or properties or children.  For functions and global variables, it will create a static class filled with static variables and static methods which encapsulates the project.   This class is named after the project.  When the output is completed, these files can be added to the "SK8InJava.µ" Metrowerks Java project and run.  I will go over the major problems and issues you may encounter while converting in the following paragraphs.
  13.  
  14. 1. The conversion will only take a project that is defined as SK8Script Text Files.  This means you cannot develop your project using the UI, save it and hope for a conversion.  Save as text won't work either, because that relies on lower level mechanisms of the existing SK8 implementation.  Most major projects have been using text files, though, to avoid problems with the sk8 save mechanism and to work collaboratively.  This mechanism is designed for them.
  15.  
  16. 2. The Java runtime for SK8 is not complete and is limited by the capabilities of Java.  For example, it can only properly handle rectangles in terms of arbitrary containment.  There is no cursor control in Java.  Nor is there the ability to draw "XOR" lines so there is no zoomrects or non-live dragging.  We recommend that you closely examine the number and kind of SK8 objects you use and do your best to limit them.  If you need to, feel free to write your own version of SK8 objects in Java and pass them along.
  17.  
  18. 3. Java has a Class/Instance based object model while SK8 follows a Prototype model.  In converting to Java, objects are converted to classes and a static variable with an instance of this class is created in the "project class".  Note that this is a good first approximation, but not 100% accurate.  Creation relations do not exist in Java (e.g. automatically copying the contents of actors).  We therefore generate a constructor for each class to handle the copying of an actors contents and to handle tags.  This output may require tweaking in some cases.
  19.  
  20. Java's classes follow a single inheritance model.  Anytime you use multiple inheritance (through adding parents), you will need to rewrite your code or make use of the Java "interfaces" mechanism.
  21.  
  22. In addition, classes will not call the "initialize" method, they will call the constructor.  You therefore have to move your initialize code into the generated constructor.
  23.  
  24. 4. Java is a strongly typed language and although the converter will do it's best to induct the type information, it is not guaranteed accurate.  Thus you will likely have to add some type information.  See the section below on how to add type information to your code.
  25.  
  26. 5. Media on Java is limited to standard internet file types.  Therefore, it will not properly convert, say, imagerenderers which contain PICTs.  You will have to hand convert the PICT file or resource into a GIF or JPEG file and hand code the proper setup in Java.  See the section below on defining Java media for more information.
  27.  
  28. 6. Some syntaxes will not translate.  In particular path expressions with “before” or “after” in them will not convert properly.  Secondly path expressions in comparators such as "if x = item n whose fillcolor is blue in the stage" will not work.  Keyword arguments do not translate properly as there is no keyword syntax in Java.  We have come up with a workaround for the simple case of "physical" to simply add this value as a required argument.  You have to remove your own keywords or handle them in Java on a case by case basis.
  29.  
  30. 7. Java is case sensitive.  SK8 is not.  Therefore all sk8 objects, handlers, etc. are output in all lower case.  This goes against Java convention, but it is at least consistent and possible.  You can, of course, do global search and replaces in your code if you wish to follow case rules.
  31.  
  32. 8. Actors are not collections in Java.  Therefore you must always say "item n in the contents of stage" rather than "item n in stage" to have it convert properly.
  33.  
  34.  
  35. The Process
  36. -----------
  37. Now that I've made a lot of caveats.  Here's how you do it.  
  38.  
  39. 1.  Type into the messagebox: "set MaintainTypeTables to true"  This tells the compiler to store tables of type information as you load your project.  It will use this type information to do a better conversion to Java.
  40.  
  41. 2.  Create and load your project as usual.  This step isn't required, but the translator can make use of your running project to do type inference.  It is highly suggested
  42.  
  43. 3. Now type the form:
  44. convertToJava of {“file1”, “file2”, ..., “filen”}, “outputdirectory:”
  45.  
  46. This will convert the set of files and output the Java classes in the specified directory.  Note that you have to do ALL of the files at once.   The convertor is not smart enough to selectively replace code in an existing translation.
  47.  
  48. Optionally you can then call:
  49. indentJavaDirectory of “outputdirectory:”
  50.  
  51. This will nicely format all of your outputted files, indenting them properly.
  52.  
  53. 4. Take a copy of the SK8InJava folder and replace the files in the User Project directory your newly output class files.  Then, start up Metrowerks and open the “SK8InJava.µ “ project.  Replace the existing user project files with your own.  
  54.  
  55. 5. In order to link your project with the rest of the SK8 runtime, go to the "Applet Glue.java" file and replace the "TestProj" reference to the name of your project.  There is a comment there explaining what to do.
  56.  
  57. 6. Now do a make and start fixing things. :)
  58.  
  59. 7. Note, if you close your project, call "ClearTypeTables()" to remove any old information about your projects types.   Do this before reloading your project.
  60.  
  61. Adding Type Information
  62. -----------------------
  63. Because Java is strongly typed, you will have to add type information to your code in order to get the conversion to compile.  Alternatively, you can add type info to your SK8script, which will be used in the conversion process.  The converter uses whatever information you give it along with information it knows about sk8 to induct as much of the type information as possible.  To do this induction it looks closely at the types of your function arguments, globals and properties to determine the types of your locals.  Therefore, at minimum you'll want to add type information to your handler and function arguments as well as your global and property definitions.  Here's how:
  64.  
  65. For functions and handlers you can add a parenthetical statement with the type after each argument.  You can also add a "returning a" clause to specify what type of return value (if any) you will return. 
  66.  
  67. on specialAdd of xx (a float), yy (an integer) returning an integer
  68.     return ((round of xx) + yy + random(10))
  69. end specialAdd
  70.  
  71. To add types to global variable's you can add a parenthetical type as follows:
  72. Global DebuggingOn (a boolean) = true
  73.  
  74. For properties add a keyword with type argument:
  75. addproperty superRectangle, 'speed' with type integer
  76.  
  77. Usually, doing this will If you wish, you can also add type information for local variables in the following ways:
  78.  
  79. on foo returning an integer
  80.    local xx (a float) = 1        --One way of explicitly declaring a local
  81.    set yy (an integer) to 2      --Another way of implicitly declaring a local
  82.  
  83.    --and here is how you declare a type for a repeat loop variable
  84.    repeat with curWindow (an actor) in the contents of the stage
  85.       sendtolog of curWindow's fillcolor
  86.    end repeat
  87.     return ((round of xx) + yy + random(10))
  88. end foo
  89.  
  90. Java Media
  91. ----------
  92. There is a difference in classes between SK8 and Java regarding media.  Mainly, the only types of media an imagerenderer can take are JPEG and GIF files.  The only type of media a sound can take is an AU file.  You will have to convert all of your media to these types.  You should replace your imagerenderers and sounds as follows.
  93.  
  94. public static imagerenderer picture1 = new imagerenderer("folder/filename.gif");
  95. public static soundrsrc sound1 = new soundrsrc("folder1/folder2/sound.au");
  96.  
  97. Note the base for the directory lookup is in the "Document Base".  In other words, put your folders with your media in the same as the invoking html file.
  98.